<?php
/**
 * MYSQLi类库
 * @Copyright (C) 2019 比物福API All rights reserved.
 * @License http://tool.bitefu.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
class db{
	public $connid;public $querynum = 0;
	public function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0,$db_charset='utf8'){
	    $port=3306;
		if(!$this->connid =mysqli_connect($dbhost.':'.$port,$dbuser,$dbpw)){echo $this->error();}
        mysqli_query($this->connid,"SET NAMES '".$db_charset."'");
        if($dbname){mysqli_query($this->connid,"use ".$dbname);}
		return $this->connid;
	}
	public function select_db($dbname){return mysqli_query($this->connid,"use ".$dbname);}
	public function query($sql){
		if(!($query = mysqli_query($this->connid,$sql))){$this->halt('MySQL Query Error', $sql);}$this->querynum++;return $query;
	}
    public function get_all($sql, $expires = 3600, $dbname = ''){$query = $this->query($sql, $expires, $dbname);$res=array();while($rs = $this->fetch_array($query)){$res[]=$rs;}return $res ;}
	public function get_one($sql, $expires = 3600, $dbname = ''){$query = $this->query($sql, $expires, $dbname);$rs = $this->fetch_array($query);return $rs ;}
	public function fetch_array($query,$type="assoc"){if(!in_array($type,array("assoc",'array',"row")))return;$funcname="mysqli_fetch_".$type;return $funcname($query);}
	public function affected_rows(){return mysqli_affected_rows($this->connid);}
	public function free_result($query) {return mysqli_free_result($query);}
	public function insert_id() {return mysqli_insert_id($this->connid);}
	public function close(){return mysqli_close($this->connid);}
	public function error(){return @mysqli_error($this->connid);}
	public function errno(){return intval(@mysqli_errno($this->connid)) ;}
	public function halt($message = '', $sql = '')	{exit("MySQL Query:$sql <br> MySQL Error:".$this->error()." <br> MySQL Errno:".$this->errno()." <br> Message:$message");}
}
?>